
package com.rong.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <pre>
 * 作者: KING  
 * 创建时间:  2012-12-8  
 * 修改时间:  2012-12-8
 * 功能说明:  访问权限过滤
 * 修改记录:
 * </pre>
 * 
 * @author king
 * @version v0.1
 * @since
 * @see
 */
public class AuthorityFilter implements Filter
{

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy()
	{
		// TODO Auto-generated method stub

	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter( ServletRequest req , ServletResponse res ,
			FilterChain chain ) throws IOException , ServletException
	{
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String url = request.getRequestURL().toString();
		url = url.toLowerCase();
		String p = request.getParameter("p");
		Object loginUser = request.getSession().getAttribute("loginUser");
		if( null == loginUser )
		{

			if( url.indexOf("/service") > 0 )
			{
				if( "login".equals(p) || "logout".equals(p))
				{
					chain.doFilter(req, res);
				}
				else
				{
					response.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
					out.print("{\"result\":-2,\"登陆超时，请重新登陆\":\"timeout\"}");
					out.flush();
					out.close();
				}	
			}
			else
			{
				chain.doFilter(req, res);
			}	
		}
		else
		{
			chain.doFilter(req, res);
		}	
	}

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init( FilterConfig config ) throws ServletException
	{
		// TODO Auto-generated method stub

	}

}
